/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control_escolar;

import static control_escolar.Horario.NExp_Alumno;
import static control_escolar.inicio.Calificaciones;
import static control_escolar.inicio.alumno_inicio;
import static control_escolar.inicio.constancia_inicio;
import static control_escolar.inicio.jMenu2;
import static control_escolar.inicio.jMenuItem6;
import static control_escolar.inicio.kardex_inicio;
import static control_escolar.inicio.maestro_inicio;
import static control_escolar.inicio.materia_inicio;
import static control_escolar.inicio.reporte1;
import static control_escolar.inicio.reporte2;
import static control_escolar.inicio.reporte3;
import static control_escolar.log.claves_sesion2;
import static control_escolar.log.expedientes;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author luis carlos piri
 */
public class Horario_Maestro extends javax.swing.JFrame {

    /**
     * Creates new form Horario
     */
    public Horario_Maestro() {
        initComponents();
        //DIAS DE LA SEMANA
        this.Dias_semana.addItem("Seleccione");
        this.Dias_semana.addItem("Lunes");
        this.Dias_semana.addItem("Martes");
        this.Dias_semana.addItem("Miercoles");
        this.Dias_semana.addItem("Jueves");
        this.Dias_semana.addItem("Viernes");
        //HORA INICIAL
        this.H_Inicial.addItem("Seleccione");
        this.H_Inicial.addItem("07:00AM");
        this.H_Inicial.addItem("08:00AM");
        this.H_Inicial.addItem("09:00AM");
        this.H_Inicial.addItem("10:00AM");
        this.H_Inicial.addItem("11:00AM");
        this.H_Inicial.addItem("12:00AM");
        this.H_Inicial.addItem("01:00PM");
        this.H_Inicial.addItem("02:00PM");
        this.H_Inicial.addItem("03:00PM");
        //DURACION DE LA CLASE
        this.Duracion.addItem("Seleccione");
        this.Duracion.addItem("00:30");
        this.Duracion.addItem("01:00");
        this.Duracion.addItem("01:30");
        this.Duracion.addItem("02:00");

        setLocationRelativeTo(null);
    }

    @Override
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("cron-programa-de-reloj-de-archivo-icono-6327-96.png"));
        return retValue;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jLabel1 = new javax.swing.JLabel();
        Materia = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        H_Inicial = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        Duracion = new javax.swing.JComboBox();
        Ingresar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        horario = new javax.swing.JTable();
        generar_horario = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        Clave_maestro = new javax.swing.JTextField();
        Dias_semana = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Horario Maestro");
        setIconImage(getIconImage());

        jLabel1.setText("Materias");

        Materia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MateriaActionPerformed(evt);
            }
        });

        jLabel2.setText("Dias");

        jLabel3.setText("Hora Inicial");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, H_Inicial, org.jdesktop.beansbinding.ObjectProperty.create(), H_Inicial, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jLabel4.setText("Duracion");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Duracion, org.jdesktop.beansbinding.ObjectProperty.create(), Duracion, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        Ingresar.setText("Ingresar");
        Ingresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IngresarActionPerformed(evt);
            }
        });

        horario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Materia", "Dia", "Hora inicial", "Duracion"
            }
        ));
        jScrollPane1.setViewportView(horario);

        generar_horario.setText("Generar horario");
        generar_horario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generar_horarioActionPerformed(evt);
            }
        });

        jButton3.setText("Regresar");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton3MousePressed(evt);
            }
        });
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel5.setText("Clave del Maestro");

        Clave_maestro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                Clave_maestroKeyTyped(evt);
            }
        });

        jLabel6.setText("GENERACION DE HORARIO PARA DOCENCIA");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(Clave_maestro, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(H_Inicial, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(43, 43, 43)
                                                .addComponent(jLabel4))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(45, 45, 45)
                                                .addComponent(Duracion, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(Ingresar))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(Dias_semana, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(30, 30, 30))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 585, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(Materia, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addGap(52, 52, 52)
                        .addComponent(generar_horario)
                        .addGap(25, 25, 25))))
            .addGroup(layout.createSequentialGroup()
                .addGap(184, 184, 184)
                .addComponent(jLabel6)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Clave_maestro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Dias_semana, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Materia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(H_Inicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Duracion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Ingresar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(generar_horario))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MateriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MateriaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_MateriaActionPerformed

    private void jButton3MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MousePressed
        // TODO add your handling code here:
        new inicio().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton3MousePressed

    private void IngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IngresarActionPerformed
        // TODO add your handling code here:
        try {

            Ingresar();
            JOptionPane.showMessageDialog(null, "Se Agrego Correctamente");
        } catch (IOException ex) {
            Logger.getLogger(Horario_Maestro.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_IngresarActionPerformed

    private void generar_horarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generar_horarioActionPerformed
        try {
            // TODO add your handling code here:
            horario();
        } catch (IOException ex) {
            Logger.getLogger(Horario_Maestro.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_generar_horarioActionPerformed

    private void Clave_maestroKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Clave_maestroKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();

        if (Character.isLetter(c)) {
            evt.consume();
        }
    }//GEN-LAST:event_Clave_maestroKeyTyped

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        validando();
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Horario_Maestro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Horario_Maestro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Horario_Maestro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Horario_Maestro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Horario_Maestro().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JTextField Clave_maestro;
    private javax.swing.JComboBox Dias_semana;
    private javax.swing.JComboBox Duracion;
    private javax.swing.JComboBox H_Inicial;
    private javax.swing.JButton Ingresar;
    private javax.swing.JTextField Materia;
    private javax.swing.JButton generar_horario;
    private javax.swing.JTable horario;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void Ingresar() throws IOException {
        PrintWriter printwriter = new PrintWriter(new FileWriter("src/ArchivosTXT/Horario_Maestro.txt", true));
        printwriter.print(Clave_maestro.getText() + ";");
        printwriter.print(Materia.getText() + ";");
        printwriter.print(Dias_semana.getSelectedItem() + ";");
        printwriter.print(H_Inicial.getSelectedItem() + ";");
        printwriter.print(Duracion.getSelectedItem() + ";");
        printwriter.flush();
        Clave_maestro.setText("");
        Materia.setText("");
        Dias_semana.setSelectedItem("seleccione");
        H_Inicial.setSelectedItem("seleccione");
        Duracion.setSelectedItem("seleccione");
    }

    public void horario() throws IOException {
        String EXP = Clave_maestro.getText();
        int o = 0;
        int a = 0;
        int b = 0;
        int c = 0;
        String expediente;
        try {
//columna clave de la materia            
            BufferedReader clave = new BufferedReader(new FileReader("src/ArchivosTXT/Horario_Maestro.txt"));
            while ((expediente = clave.readLine()) != null) {
                if (expediente.startsWith(EXP)) {
                    expediente = expediente.replace(EXP, "");
                    String[] horarios = expediente.split(";");
                    horario.setValueAt(horarios[1], o, 0);
                    horario.paintImmediately(horario.getX(), horario.getY(), horario.getWidth(), horario.getHeight());
                    o = o + 1;
                    horario.setValueAt(horarios[2], a, 1);
                    horario.paintImmediately(horario.getX(), horario.getY(), horario.getWidth(), horario.getHeight());
                    a = a + 1;
                    horario.setValueAt(horarios[3], b, 2);
                    horario.paintImmediately(horario.getX(), horario.getY(), horario.getWidth(), horario.getHeight());
                    b = b + 1;
                    horario.setValueAt(horarios[4], c, 3);
                    horario.paintImmediately(horario.getX(), horario.getY(), horario.getWidth(), horario.getHeight());
                    c = c + 1;
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(kardex.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   public void validando() {
        String EXP = log.expedientes.getText();
        String pwd = log.claves_sesion2.getText();
        if (expedientes.getText().length() != 0 && claves_sesion2.getText().length() != 0) {
            try {

                BufferedReader alumnos = new BufferedReader(new FileReader("src/ArchivosTXT/Alumnos.txt"));
                String Materias;
                while ((Materias = alumnos.readLine()) != null) {

                    if (Materias.startsWith(EXP)) {
                        String[] Al = Materias.split(";");
                        if (Al[0].equals(EXP) && Al[22].equals(pwd)) {
                            
                            new inicio().setVisible(true);
                            this.setVisible(false);
                            inicio.constancia_inicio.setVisible(false);
                            inicio.reporte1.setVisible(false);
                            inicio.reporte2.setVisible(true);
                            inicio.reporte3.setVisible(false);
                            inicio.kardex_inicio.setVisible(true);
                            inicio.jMenu2.setVisible(false);
                        } else {
                            JOptionPane.showMessageDialog(null, "Datos Erroneos");
                        }
                    }
                }

                BufferedReader maestros = new BufferedReader(new FileReader("src/ArchivosTXT/Maestros.txt"));
                String Maestros;
                while ((Maestros = maestros.readLine()) != null) {
                    if (Maestros.startsWith(EXP)) {
                        String[] Ma = Maestros.split(";");
                        if (Ma[0].equals(EXP) && Ma[12].equals(pwd)) {
                            
                            new inicio().setVisible(true);
                            this.setVisible(false);
                            inicio.constancia_inicio.setVisible(false);
                            inicio.reporte1.setVisible(false);
                            inicio.reporte2.setVisible(true);
                            inicio.reporte3.setVisible(false);
                            inicio.kardex_inicio.setVisible(false);
                            inicio.jMenu2.setVisible(true);
                            inicio.alumno_inicio.setVisible(false);
                            inicio.maestro_inicio.setVisible(false);
                            inicio.materia_inicio.setVisible(false);
                            inicio.Calificaciones.setVisible(true);
                            inicio.jMenuItem6.setVisible(false);
                            //inicio.jMenuItem7.setVisible(false);

                        } else {
                            JOptionPane.showMessageDialog(null, "Datos Erroneos");
                        }
                    }
                }
                if (EXP.equals("Admin") && pwd.equals("Citrus")) {
                    
                    new inicio().setVisible(true);
                    this.setVisible(false);
                    constancia_inicio.setVisible(true);
                    inicio.reporte1.setVisible(true);
                    inicio.reporte2.setVisible(true);
                    inicio.reporte3.setVisible(true);
                    inicio.kardex_inicio.setVisible(true);
                    inicio.jMenu2.setVisible(true);
                    inicio.alumno_inicio.setVisible(true);
                    inicio.maestro_inicio.setVisible(true);
                    inicio.materia_inicio.setVisible(true);
                    inicio.Calificaciones.setVisible(true);
                    inicio.jMenuItem4.setVisible(true);
                    inicio.jMenuItem6.setVisible(true);
                    inicio.jMenuItem7.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Datos Erroneos");
                }
            } catch (IOException x) {
            }
        } else {
            JOptionPane.showMessageDialog(null, "Campos Requeridos");
        }
}
}
